How to get N random string from a {a1|a2|a3} format string?

Posted by Pentium10 on Stack Overflow See other posts from Stack Overflow or by Pentium10
Published on 2010-06-03T10:48:06Z Indexed on 2010/06/03 10:54 UTC
Read the original article Hit count: 236

Filed under:
|
|
|

Take this string as input:

string s="planets {Sun|Mercury|Venus|Earth|Mars|Jupiter|Saturn|Uranus|Neptune}"

How would I choose randomly N from the set, then join them with comma. The set is defined between {} and options are separated with | pipe. The order is maintained.

Some output could be:

string output1="planets Sun, Venus";
string output2="planets Neptune";
string output3="planets Earth, Saturn, Uranus, Neptune";
string output4="planets Uranus, Saturn";// bad example, order is not correct

Java 1.5

© Stack Overflow or respective owner

Related posts about java

Related posts about regex